package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.common.base.service.impl.BaseServiceImpl;
import com.example.common.utils.ApiResult;
import com.example.dao.TEquipmentPartMapper;
import com.example.dao.TPartPlanMapper;
import com.example.dao.TPlanMapper;
import com.example.dao.TSourceMapper;
import com.example.model.entity.*;
import com.example.model.query.TPlanQuery;
import com.example.model.query.TSourceQuery;
import com.example.service.TPlanService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 告警预案表 Service接口实现类
 *
 * @author zhangr
 * @Description Created on 2020-12-17
 */
@Service
public class TPlanServiceImpl extends BaseServiceImpl<TPlanMapper,TPlan> implements TPlanService {

    @Autowired
    private TPlanMapper planMapper;

    @Autowired
    private TEquipmentPartMapper equipmentPartMapper;

    @Autowired
    private TPartPlanMapper partPlanMapper;

    @Override
    public ApiResult<TPlanQuery> pageUserVoByQuery(TPlanQuery query) {
        query.setOptimizeCountSql(false);
        Long current = query.getCurrent();
        query.setCurrent(1L);
        QueryWrapper<TPlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TPlan::getIsValid,1L).orderByDesc(TPlan::getCreateDate);
        if(StringUtils.isNotBlank(query.getPlanNum())){
            queryWrapper.like("plan_num",query.getPlanNum());
        }
        if(StringUtils.isNotBlank(query.getPlanName())){
            queryWrapper.like("plan_name",query.getPlanName());
        }
        Integer total = planMapper.selectCount(queryWrapper);
        query.setTotal(total);
        query.setCurrent(current);
        planMapper.selectPage(query,queryWrapper);
        return new ApiResult<>(query);
    }

    @Override
    public ApiResult<List<TPlan>> getList(String id){
        if(StringUtils.isBlank(id)){
            return new ApiResult<>(33,"id为空");
        }
        QueryWrapper<TPartPlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TPartPlan::getIsValid,1L).eq(TPartPlan::getPartId,id);
        List<TPartPlan> resultlist = partPlanMapper.selectList(queryWrapper);
        List<TPlan> list = planMapper.selectBatchIds(resultlist);
        return new ApiResult<>(list);
    }

    @Override
    public ApiResult<List<TPlan>> getList(){
        QueryWrapper<TPlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TPlan::getIsValid,1L);
        List<TPlan> list = planMapper.selectList(queryWrapper);
        return new ApiResult<>(list);
    }



    @Override
    public ApiResult<Boolean> saveObj(TPlan plan){
        if(StringUtils.isBlank(plan.getPlanNum())){
            return new ApiResult<>(-1,"预案编号不能为空");
        }
        QueryWrapper<TPlan> planQueryWrapper = new QueryWrapper<>();
        planQueryWrapper.lambda().eq(TPlan::getIsValid,1L).eq(TPlan::getPlanNum,plan.getPlanNum());
        int num = planMapper.selectCount(planQueryWrapper);
        if(num > 0){
            return new ApiResult<>(-1,"预案编号已被占用");
        }
        plan.setCreateDate(LocalDateTime.now());
        planMapper.insert(plan);
        return new ApiResult<>(Boolean.TRUE);
    }
}

